package com.szly.phm.common.utils;

import cn.hutool.poi.excel.ExcelReader;
import cn.hutool.poi.excel.ExcelUtil;
import org.springframework.core.io.ClassPathResource;
import org.springframework.core.io.Resource;

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @Description : ${创建类}
 * @Author : chengjiang
 * @Date: 2021/7/21 11:38
 */
public class FileUtils {

    public static InputStream getInputStrem(String filePath) throws IOException {
        //目录分隔符，不要使用"/"或者"\"
        var spe = File.separator;
        String relativeFilePath = "resources" + spe + filePath;
        var file = new File(".", relativeFilePath);
        if (file.exists()) {
            return new FileInputStream(file);
        } else {
            Resource resource2 = new ClassPathResource(filePath);
            if (resource2.exists()) {
                return resource2.getInputStream();
            }
        }
        System.out.println("内外文件都不存在");
        return null;
    }

    public static Map<String, List<Object>> getJudgmentExcel() {
        var map = new HashMap<String, List<Object>>();
        try {
            var filePa = "excel" + File.separator + "phm研判模型字典.xlsx";
            var is = FileUtils.getInputStrem(filePa);
            if (is != null && is.available() > 0) {
                ExcelReader reader = ExcelUtil.getReader(is);
                List<List<Object>> readAll = reader.read();
                for (int i = 0, len = readAll.size(); i < len; i++) {
                    var hang = readAll.get(i);
                    for (int j = 0, len2 = hang.size(); j < len2; j++) {
                        if (map.containsKey(hang.get(0))) {
                            if (!hang.get(0).equals(hang.get(j))) {
                                map.get(hang.get(0)).add(hang.get(j));
                            }
                        } else {
                            var list = new ArrayList<>();
                            map.put((String) hang.get(0), list);
                        }
                    }
                }
            }
        } catch (
                IOException e) {
            e.printStackTrace();
        }
        return map;
    }

}
